<?php


namespace app\admin\controller;

use think\facade\Request;
use tools\Compress;

class UpfileController extends Controller
{
    protected $authExcept = [
        'upload','uploadimg'
    ];

    //保存路径
    protected $save_path = 'uploads/attachment/';
    protected $check = [
      
        'file'=>[
            'fileSize'=> '10240000',
            'fileExt' =>'jpg,png,gif,docx,psd,doc,PDF,pdf,rar,zip', //可上传的格式
        ]
    ];
    public function upload()
    {
        $files = Request::file();
        try {
             validate($this->check)->check($files);

            $file = $files['file'];
            $info = \think\facade\Filesystem::putFile($this->save_path, $file);
            if ($info) {
                if(strpos($file->getOriginalMime(),'image') !== false){
                    $imgurl = public_path() . str_replace("\\", '/', $info);
                    (new Compress($imgurl, 1))->compressImg($imgurl);
                }
            } else {
                return error($info->getError());
            }
        } catch (\think\exception\ValidateException $e) {
           return error($e->getMessage()) ;
        }
        $data = [
            'original_name' =>  $file->getOriginalName(),
            'url'     => '/' . str_replace("\\", '/',  str_replace("//", '/',  $info)),
            'type'     => $file->extension(),
            'mime'          => $file->getOriginalMime(),
            'size'          => format_size($file->getSize()),
            'save_name'     => $this->setSaveName($file->hashName()),

        ];

        return $info ? success('上传成功', '', $data) :  error($file->getError());
    }

    public function setSaveName($value)
    {
        $array = explode('\\', $value);
        return end($array);
    }

    // public function uploadImg()
    // {
    //     $checkStr = $this->getStr($this->imgCheck);
    //     $files = Request::file('file');
    //     dd($files->getOriginalMime());


    //     try {
    //         $aa = validate(['image' => $checkStr, 'file' => $checkStr])->check($files);
    //         dd($aa);
    //         $file = $files['file'];
    //         $info = \think\facade\Filesystem::putFile($this->save_path, $file);
    //         if ($info) {
    //             $imgurl = public_path() . str_replace("\\", '/', $info);
    //             // 压缩图片
    //             $compress = (new Compress($imgurl, 1))->compressImg($imgurl);
    //             $data['url'] = '/' .  str_replace("\\", '/', $info);
    //             return $compress ? success('上传成功', '',  $data) : error('上传失败');
    //         } else {
    //             return error($info->getError());
    //         }
    //     } catch (\think\exception\ValidateException $e) {
    //         echo $e->getMessage();
    //     }
    // }

    // protected function checkImg(array $data)
    // {
    //     //实例化验证器
    //     $validate = Validate::make([
    //         'size'   => '<:' . $this->max_size,
    //         'suffix' => 'in:jpg,png,',
    //     ]);
    //     //定义错误提示
    //     $validate->message([
    //         'size'   => '图片太大',
    //         'suffix' => '图片格式不合法',
    //     ]);
    //     //验证数据
    //     if (!$validate->check($data)) {
    //         return error($validate->getError());
    //     }
    // }
    public function getStr(array $param, string $str = '')
    {
        end($param);
        $last_key = key($param);
        foreach ($param as $key => $vo) {
            $str .= $key . ':';
            if (is_array($vo)) {
                $last_item = end($vo);
                foreach ($vo as  $item) {
                    $str .= $item;
                    if ($last_item != $item) {
                        $str .= ',';
                    }
                }
            } else {
                $str .= $vo;
            }
            if ($key != $last_key) {
                $str .= '|';
            }
        }
        return $str;
    }
}
